package com.ns.basic.dict.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.basic.dict.bo.DictItemBO;
import com.ns.basic.dict.bo.DictItemPageBO;
import com.ns.basic.dict.dto.DictItemPageDto;
import com.ns.basic.dict.entry.DictItemEntry;
import com.ns.basic.dict.service.IDictItemEntryService;
import com.ns.basic.dict.vo.DictItemEntryVO;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.current.config.SysConst;
import com.ns.core.utils.sg.ResultOk;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 字典对外接口
 *
 * @author lw
 * @since 2024/08/28
 */
@RestController
@RequestMapping("/dict/item/")
@RequiredArgsConstructor
@CrossOrigin
public class DictItemEntryController {

    private final IDictItemEntryService dictItemEntryService;

    /**
     * 增
     *
     * @param bo 参数
     */
    @PostMapping("add")
    @ReqLog(module = "字典项模块", name = "添加")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    @Transactional(rollbackFor = Exception.class)
    public ResultOk add(@RequestBody DictItemBO bo) {
        DictItemEntry itemEntry = new DictItemEntry();
        BeanUtils.copyProperties(bo, itemEntry);
        itemEntry.setSort(dictItemEntryService.count() + 1);
        return ResultOk.getByBoolean(dictItemEntryService.saveOrUpdate(itemEntry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "字典项模块", name = "删")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk delete(Long id) {
        return ResultOk.getByBoolean(dictItemEntryService.removeById(id));
    }

    /**
     * 删 - 根据id列表
     *
     * @param bo 参数
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "字典项模块", name = "批量删")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(dictItemEntryService.removeBatchByIds(bo.getIds()));
    }

    /**
     * 改
     *
     * @param bo 参数
     */
    @PostMapping("update")
    @ReqLog(module = "字典项模块", name = "改")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk update(@RequestBody DictItemBO bo) {
        DictItemEntry itemEntry = new DictItemEntry();
        BeanUtils.copyProperties(bo, itemEntry);
        return ResultOk.getByBoolean(dictItemEntryService.updateById(itemEntry));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "字典项模块", name = "查")
    public ResultOk getById(long id) {
        DictItemEntry dictEntry = dictItemEntryService.getById(id);
        DictItemEntryVO vo = new DictItemEntryVO();
        BeanUtils.copyProperties(dictEntry, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查 - 集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "字典项模块", name = "集合")
    public ResultOk getList(@RequestBody DictItemPageBO bo) {
        DictItemPageDto dto = new DictItemPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<DictItemEntryVO> page = dictItemEntryService.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查 - 集合
     *
     * @param dictId 字典id
     */
    @GetMapping("list")
    @ReqLog(module = "字典项模块", name = "集合")
    public ResultOk getList(Long dictId) {
        return ResultOk.getSuccessData(dictItemEntryService.listByDictId(dictId));
    }

    /**
     * 查
     *
     * @param code 参数
     */
    @GetMapping("/{code}")
    @ReqLog(module = "字典项模块", name = "通过字典code查询")
    public ResultOk getByDict(@PathVariable String code) {
        return ResultOk.getSuccessData(dictItemEntryService.listByDictCode(code));
    }
}
